package com.diyphonegadgets.freeblog.network;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.apache.http.client.methods.HttpRequestBase;

import android.content.Context;

import com.diyphonegadgets.freeblog.data.object.Article;
import com.diyphonegadgets.freeblog.data.parser.ParsingManager;



public class ResponseBuilder {
	private final static String TAG = "REPONSE_BUILDER";



	
	public static List<Article> getArticles(HttpRequestBase request, Context context) throws Exception{
		//String response = new String(getData(request));
		
		String response = new String (getDataFromAssets(context));
		
		if(response == null){
			return null;
		}
		return ParsingManager.buildArticlesList(response);
	}
	
	private static byte[] getDataFromAssets(Context context){
		
		 InputStream input = null;
			try {
				input = context.getAssets().open("rss.xml");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	            // myData.txt can't be more than 2 gigs.
	            int size = 0;
				try {
					size = input.available();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            byte[] buffer = new byte[size];
	            try {
					input.read(buffer);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	 
	            return buffer;


		
	}
	

	private static byte[] getData (HttpRequestBase httpRequest) throws Exception{
		try {        
			HttpResponse response = HttpConnector.getHttpResponse(httpRequest);
			int responseCode = response.getCode();
			
			if(responseCode == HttpCode.OK){
				response.readBody();
			}
			else{
				response.close();
				throw new Exception("HTTP request failed [" + responseCode + "]"/*, CommunicationException.HTTP_ERROR*/);
			}
			byte[] data = response.getByteContent();
			response.close();
			return data;
        }
        catch (Exception e){
            e.printStackTrace();
            throw new Exception ("HTTP request failed"/*, CommunicationException.HTTP_ERROR*/);
        }
	 }
	
	
	/*
	 * This method creates an HTTP connection with the remote RSS host pointed by the user-specified
	 * URL and returns the server's response as a stream of XML data.
	 */
	public static InputStream getRSSData(HttpRequestBase httpRequest) {

		try {
			URL url = httpRequest.getURI().toURL();

			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream is = conn.getInputStream();
				return is;
			} else {
				return null;
			}
		} catch (Exception e) {

			return null;
		}

	}
	
	

}
